﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;

using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.Common;
namespace Think.Lenovo.Com.DataAccess
{
    public class Roles
    {
        public RoleModel GetRole(int id)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_role where id=" + id);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            RoleModel model = new RoleModel();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Int32 i;
                        model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                        model.Code = Convert.ToString(ds.Tables[0].Rows[0]["Code"]);
                        model.Name = Convert.ToString(ds.Tables[0].Rows[0]["Name"]);
                        Int32.TryParse(ds.Tables[0].Rows[0]["Seq"].ToString(), out i);
                        model.Seq = i;
                    }
                }
            }
            return model;
        }

        public LinkedList<RoleModel> GetRoles()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_role order by seq");
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            LinkedList<RoleModel> list = new LinkedList<RoleModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        Int32 i;
                        RoleModel model = new RoleModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.Code = Convert.ToString(row["Code"]);
                        model.Name = Convert.ToString(row["Name"]);
                        Int32.TryParse(row["Seq"].ToString(), out i);
                        model.Seq = i;
                        list.AddLast(model);
                    }
                }
            }
            return list;
        }

        public long AddRole(RoleModel model)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append("tbl_role (Code, Name, Seq)")
                .Append("values(@Code,@Name,@Seq)");


            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@Code",MySqlDbType.VarChar,50),
                    new MySqlParameter("@Name",MySqlDbType.VarChar,32),
                    new MySqlParameter("@Seq",MySqlDbType.Int32)
                };

            ps[0].Value = model.Code;
            ps[1].Value = model.Name;
            ps[2].Value = model.Seq;

            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;

        }

        public int UpdateRole(RoleModel model)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            //sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Update ")
                .Append(" tbl_role Set Code=@Code, Name=@Name, Seq=@Seq ")
                .Append(" Where Id=@Id");


            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@Code",MySqlDbType.VarChar,50),
                    new MySqlParameter("@Name",MySqlDbType.VarChar,32),
                    new MySqlParameter("@Seq",MySqlDbType.Int32),
                    new MySqlParameter("@Id",MySqlDbType.Int32)
                };

            ps[0].Value = model.Code;
            ps[1].Value = model.Name;
            ps[2].Value = model.Seq;
            ps[3].Value = model.Id;
            int affect = MysqlHelper.Update(conn, sql.ToString(), ps);
            return affect;

        }
    }
}
